<?php
/**
 * @file jiemengcontroller.php
 * @author LinLin
 * @date 2016年4月19日 上午10:02:17
 * @brief 
 */
namespace haibao\cms\business\control;

use haibao\frame\data\query\Condition;
use haibao\frame\data\exception\DataErrorException;
use haibao\frame\http\UserControl;
use haibao\frame\data\query\Pager;

class JiemengController extends Base{

    const PAGE_SIZE = 10;
    const JIEMENG_TOP_TAG_ID = 520;//DIY生活
    const JIEMENG_CHANNEL = 'diy';
    const JIEMENG_TAG_ID = 11354;//周公解梦tagid
    private static $_instance;
    public static $tagIdArr = array(11354,11361,11362,11363,11364,11365,11366,11367);
    public $jiemengData;
    public function __construct($updateTime='',$controlId='',$isPublish=''){
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    public static function getJiemengInstance(){
        if(!(self::$_instance instanceof self)){
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    public function getRightCommonData($dataCondition){
        $dataCondition['source'] = __CLASS__;
        $dataCondition['level'] = 1;
        $dataCondition['isProImage'] = true;
        $dataCondition['isPrimaryTag'] = true;
        $dataCondition['isReturnTag'] = true;
        $dataCondition['isTag'] = true;
        $dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
        $this->conditionTagId = $dataCondition['tagId'];
        $dataCondition['isParentTag'] = false;
        $dataCondition['isReturnPrimary'] = false;
        $this->parseTagId($dataCondition['tagId']);
        $parentId = self::JIEMENG_TOP_TAG_ID;
        list($articles,$imageColls) = $this->getArticleControl(1,$dataCondition);
        $topTag = self::getTopTags();
        $moreUrl = $moreName = '';
        if($articles){
            foreach($articles['list'] as $key=>$val){
                if(isset($articles['proImage'][$val->ArticleTag->ArticleId])){
                    if(isset($articles['proImage'][$val->ArticleTag->ArticleId][4])){
                        $articles['list'][$key]->AccessUrl = $articles['proImage'][$val->ArticleTag->ArticleId][4];
                    }else {
                        $articles['list'][$key]->AccessUrl = $articles['proImage'][$val->ArticleTag->ArticleId][0];  //图片地址
                    }
                }
                $articles['list'][$key]->ArticleUrl = $this->getArticleUrl($articles['list'][$key]->PublishId,$parentId); //文章地址
            }
            if(isset($articles['tag']) && $articles['tag']){
                foreach($articles['tag'] as $key=>$val){
                    if($key == $this->conditionTagId){
                        $moreUrl = $val['Url'];
                        $moreName = $val['TagName'];
                    }
                }
            }
        }
        if($imageColls){
            foreach($imageColls['list'] as $key=>$val){
                if(isset($imageColls['proImage'][$val->ImageCollectiveTag->ImageCollectiveId])){
                    $imageColls['list'][$key]->AccessUrl = $imageColls['proImage'][$val->ImageCollectiveTag->ImageCollectiveId][0];
                }
                $imageColls['list'][$key]->ImageCollectivePro = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'.$topTag[$parentId]['channel'].'/pics/'.$imageColls['list'][$key]->ImageCollectiveTag->ImageCollectiveId.'.htm';
            }
            if(isset($imageColls['tag']) && $imageColls['tag']){
                foreach($imageColls['tag'] as $key=>$val){
                    if($key == $this->conditionTagId){
                        $moreUrl = $val['Url'];
                        $moreName = $val['TagName'];
                    }
                }
            }
        }
        $mergeData = array();
        if(!empty($articles['list']) || !empty($imageColls['list'])){
            $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
        }
       return array($mergeData,$moreUrl,$moreName);
    }
}